package com.xt.base.action;

import java.util.Stack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

/**
 * Implementation of <strong>ActionSupport</strong> that contains convenience
 * methods for subclasses. For example, getting the current user and saving
 * messages/errors. This class is intended to be a base class for all Action
 * classes.
 * 
 * <p>
 * <a href="BaseAction.java.html"><i>�鿴Դ�ļ�</i></a>
 * </p>
 * 
 * @author ��鲨
 */
public abstract class BaseAction extends ActionSupport {
	private static final long serialVersionUID = 363533471070081536L;

	public static final String SESSIONCURRENTUSER="currentUser";
	
	public static final String QueryCondition = "queryCondition";

	protected transient final Logger log = Logger.getLogger(getClass());

	/**
	 * Convenience method to get the request
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * Convenience method to get the response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * Convenience method to get the session
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}
	
	/**
     * Get һ�� object��WebWork����������
     */
	protected String getParam(String key){
		return this.getRequest().getParameter(key);
	}
	
	/**
     * Get һ�� object��WebWork����������
     */
	protected Object get(String key){
		return ActionContext.getContext().get(key);
	}
	
	/**
     * Set һ�� object��WebWork����������
     */
	protected void set(String key,Object value){
		ActionContext.getContext().put(key, value);
	}

	
	 /**
     * Get һ�� object �� WebWork ���û� session��
     */
    protected <T extends Object> T getSessionAttr(String name) {
        return (T)ActionContext.getContext().getSession().get(name);
    }

    /**
     * Put һ�� object �� WebWork �û� session��
     */
	protected void setSessionAttr(String name, Object value) {
        ActionContext.getContext().getSession().remove(name);
        ActionContext.getContext().getSession().put(name, value);
    }
    
    /**
     *  �� Seesion Stack  pop  һ�� object 
     */
    protected Object popFromSessionStack() {
    	Stack sessionStack = (Stack)getSessionAttr("sessionStack");
    	if (sessionStack == null)  return null ;
    	if (sessionStack.empty())   return null ;

        return sessionStack.pop() ;
    }

    
    /**
     *  push  һ�� object  �� Seesion Stack 
     */
	protected void pushToSessionStack( Object value) {

    	Stack sessionStack = (Stack)getSessionAttr("sessionStack");
    	if (sessionStack == null)  
    		throw new Error("��ݴ���sessionStackʧ��!") ;
    	sessionStack.push(value);
    }

    /**
     * �ж� session Stack  �Ƿ�Ϊ��  
     */
    protected  boolean isEmptySessionStack( ) {
       	Stack sessionStack = (Stack)getSessionAttr("sessionStack");
    	if (sessionStack == null)  
    		throw new Error("sessionStack ������!") ;
    	 return sessionStack.empty()  ;
    }


    /**
     * ��� session Stack   
     */
    protected  void clearSessionStack( ) {
       	Stack sessionStack = (Stack)getSessionAttr("sessionStack");
    	if (sessionStack == null)  
    		throw new Error("sessionStack������!") ;
    	if (sessionStack.empty())   return  ;
    	sessionStack.removeAllElements();
    }

    /**
     * �� Seesion Stack  peek  һ�� object  , ���Ƽ�ʹ�� 
     */
    protected  Object peekFromSessionStack( ) {
       	Stack sessionStack = (Stack)getSessionAttr("sessionStack");
    	if (sessionStack == null)  return null ;
    	if (sessionStack.empty())   return null ;
        return sessionStack.peek() ;
    }
    
    
    
    /**
     *  ���� Seesion Stack ��Ӧ��ִֻ��һ��
     */
    protected  void createSessionStack() {
       	Stack sessionStack = (Stack)getSessionAttr("sessionStack");
    	if (sessionStack == null) {
          	Stack s = new Stack();
    		setSessionAttr("sessionStack", s) ;
    	}
    }
}
